home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Graphics Programming (2nd Edition) / Visual Basic Graphics Programming 2nd Edition.iso / OldSrc / CH5 / SRC / BOUNCE2.FRM (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1997-01-08  |  12.8 KB  |  413 lines

  1. VERSION 4.00
  2. Begin VB.Form BounceForm 
  3.    Caption         =   "Bounce1"
  4.    ClientHeight    =   5235
  5.    ClientLeft      =   1320
  6.    ClientTop       =   1110
  7.    ClientWidth     =   6870
  8.    Height          =   5925
  9.    Left            =   1260
  10.    LinkTopic       =   "Form1"
  11.    ScaleHeight     =   349
  12.    ScaleMode       =   3  'Pixel
  13.    ScaleWidth      =   458
  14.    Top             =   480
  15.    Width           =   6990
  16.    Begin VB.OptionButton Method 
  17.       Caption         =   "Erase and Redraw"
  18.       Height          =   255
  19.       Index           =   3
  20.       Left            =   3600
  21.       TabIndex        =   10
  22.       Top             =   4920
  23.       Width           =   1695
  24.    End
  25.    Begin VB.OptionButton Method 
  26.       Caption         =   "SetBitmapBits"
  27.       Height          =   255
  28.       Index           =   2
  29.       Left            =   3600
  30.       TabIndex        =   9
  31.       Top             =   4560
  32.       Width           =   1695
  33.    End
  34.    Begin VB.OptionButton Method 
  35.       Caption         =   "Save and Restore"
  36.       Height          =   255
  37.       Index           =   1
  38.       Left            =   1920
  39.       TabIndex        =   8
  40.       Top             =   4920
  41.       Width           =   1695
  42.    End
  43.    Begin VB.OptionButton Method 
  44.       Caption         =   "Cls"
  45.       Height          =   255
  46.       Index           =   0
  47.       Left            =   1920
  48.       TabIndex        =   7
  49.       Top             =   4560
  50.       Value           =   -1  'True
  51.       Width           =   1695
  52.    End
  53.    Begin VB.PictureBox Court 
  54.       AutoRedraw      =   -1  'True
  55.       Height          =   4455
  56.       Left            =   0
  57.       ScaleHeight     =   293
  58.       ScaleMode       =   3  'Pixel
  59.       ScaleWidth      =   453
  60.       TabIndex        =   6
  61.       Top             =   0
  62.       Width           =   6855
  63.    End
  64.    Begin VB.PictureBox OffScreen 
  65.       AutoRedraw      =   -1  'True
  66.       Height          =   615
  67.       Left            =   6240
  68.       ScaleHeight     =   37
  69.       ScaleMode       =   3  'Pixel
  70.       ScaleWidth      =   37
  71.       TabIndex        =   5
  72.       Top             =   4560
  73.       Visible         =   0   'False
  74.       Width           =   615
  75.    End
  76.    Begin VB.TextBox FPSText 
  77.       Height          =   285
  78.       Left            =   1440
  79.       TabIndex        =   3
  80.       Text            =   "20"
  81.       Top             =   4920
  82.       Width           =   375
  83.    End
  84.    Begin VB.TextBox BallsText 
  85.       Height          =   285
  86.       Left            =   1440
  87.       TabIndex        =   2
  88.       Text            =   "20"
  89.       Top             =   4560
  90.       Width           =   375
  91.    End
  92.    Begin VB.CommandButton CmdStart 
  93.       Caption         =   "Start"
  94.       Default         =   -1  'True
  95.       Height          =   495
  96.       Left            =   5400
  97.       TabIndex        =   0
  98.       Top             =   4620
  99.       Width           =   855
  100.    End
  101.    Begin VB.Label Label1 
  102.       Caption         =   "Frames per second:"
  103.       Height          =   255
  104.       Index           =   0
  105.       Left            =   0
  106.       TabIndex        =   4
  107.       Top             =   4920
  108.       Width           =   1455
  109.    End
  110.    Begin VB.Label Label1 
  111.       Caption         =   "Number of balls:"
  112.       Height          =   255
  113.       Index           =   1
  114.       Left            =   0
  115.       TabIndex        =   1
  116.       Top             =   4560
  117.       Width           =   1455
  118.    End
  119.    Begin VB.Menu mnuFile 
  120.       Caption         =   "&File"
  121.       Begin VB.Menu mnuFileExit 
  122.          Caption         =   "E&xit"
  123.       End
  124.    End
  125. Attribute VB_Name = "BounceForm"
  126. Attribute VB_Creatable = False
  127. Attribute VB_Exposed = False
  128. Option Explicit
  129. Const METHOD_CLS = 0
  130. Const METHOD_SAVE_AND_RESTORE = 1
  131. Const METHOD_SETBITS = 2
  132. Const METHOD_ERASE_AND_REDRAW = 3
  133. Dim DrawingMethod As Integer
  134. Dim xmax As Integer
  135. Dim ymax As Integer
  136. Dim StartX As Integer
  137. Dim StartY As Integer
  138. Dim NumBalls As Integer
  139. Dim BallR() As Integer
  140. Dim BallX() As Integer
  141. Dim BallY() As Integer
  142. Dim BallDx() As Integer
  143. Dim BallDy() As Integer
  144. Dim BallClr() As Long
  145. Dim Playing As Boolean
  146. ' ************************************************
  147. ' Generate some random data.
  148. ' ************************************************
  149. Sub InitData()
  150. Dim ball As Integer
  151. Dim r As Integer
  152. Dim clr As Integer
  153.     ' See how many balls there should be.
  154.     If Not IsNumeric(BallsText.Text) Then Exit Sub
  155.     NumBalls = CInt(BallsText.Text)
  156.     If NumBalls < 1 Then Exit Sub
  157.     ReDim BallR(1 To NumBalls)
  158.     ReDim BallX(1 To NumBalls)
  159.     ReDim BallY(1 To NumBalls)
  160.     ReDim BallDx(1 To NumBalls)
  161.     ReDim BallDy(1 To NumBalls)
  162.     ReDim BallClr(1 To NumBalls)
  163.     ' Set the initial ball data.
  164.     For ball = 1 To NumBalls
  165.         r = Int(10 * Rnd + 5)
  166.         BallR(ball) = r
  167.         BallX(ball) = Int((xmax - r + 1) * Rnd)
  168.         BallY(ball) = Int((ymax - r + 1) * Rnd)
  169.         BallDx(ball) = Int(21 * Rnd - 10)
  170.         BallDy(ball) = Int(21 * Rnd - 10)
  171.         clr = Int(15 * Rnd)
  172.         If clr >= 7 Then clr = clr + 1
  173.         BallClr(ball) = QBColor(clr)
  174.     Next ball
  175.     StartX = BallX(1)
  176.     StartY = BallY(1)
  177. End Sub
  178. ' ************************************************
  179. ' Reinitialize the data.
  180. ' ************************************************
  181. Private Sub BallsText_Change()
  182.     InitData
  183. End Sub
  184. ' ************************************************
  185. ' Start the animation.
  186. ' ************************************************
  187. Private Sub CmdStart_Click()
  188.     If Playing Then
  189.         Playing = False
  190.         CmdStart.Caption = "Stopped"
  191.         CmdStart.Enabled = False
  192.     Else
  193.         CmdStart.Caption = "Stop"
  194.         Playing = True
  195.         PlayData
  196.         Playing = False
  197.         CmdStart.Caption = "Start"
  198.         CmdStart.Enabled = True
  199.     End If
  200. End Sub
  201. ' ************************************************
  202. ' Play the animation.
  203. ' ************************************************
  204. Sub PlayData()
  205. Dim mpf As Long     ' Milliseconds per frame.
  206. Dim ball As Integer
  207. Dim next_time As Long
  208. Dim old_style As Integer
  209. Dim moving As Integer
  210. Dim r As Integer
  211. Dim r2 As Integer
  212. Dim D As Integer
  213. Dim oldx As Integer
  214. Dim oldy As Integer
  215. Dim newx As Integer
  216. Dim newy As Integer
  217. Dim i As Integer
  218. Dim j As Integer
  219. Dim frames As Integer
  220. Dim start_time As Single
  221. Dim stop_time As Single
  222. Dim bm As BITMAP
  223. Dim hbm As Integer
  224. Dim status As Long
  225. Dim wid As Long
  226. Dim hgt As Long
  227. Dim num_bits As Long
  228. Dim bytes() As Byte
  229.     ' Set FillStyle to vbSolid.
  230.     old_style = Court.FillStyle
  231.     Court.FillStyle = vbSolid
  232.     ' See how fast we should go.
  233.     If Not IsNumeric(FPSText.Text) Then _
  234.         FPSText.Text = "10"
  235.     mpf = 1000 \ CLng(FPSText.Text)
  236.     ' Erase the screen.
  237.     Court.Line (0, 0)-(xmax, ymax), Court.BackColor, BF
  238.     ' Draw the background balls.
  239.     moving = 1
  240.     For ball = 2 To NumBalls
  241.         Court.FillColor = BallClr(ball)
  242.         Court.Circle (BallX(ball), BallY(ball)), _
  243.             BallR(ball), BallClr(ball)
  244.     Next ball
  245.     ball = moving
  246.     BallX(moving) = StartX
  247.     BallY(moving) = StartY
  248.     oldx = StartX
  249.     oldy = StartY
  250.     newx = StartX
  251.     newy = StartY
  252.     BallDx(moving) = 10
  253.     BallDy(moving) = 10
  254.     r = BallR(moving)
  255.     D = 2 * r + 1
  256.     ' Prepare for the animation.
  257.     Select Case DrawingMethod
  258.         Case METHOD_CLS
  259.             ' Make the picture the background.
  260.             Court.Picture = Court.Image
  261.         
  262.         Case METHOD_SAVE_AND_RESTORE
  263.             ' Make the picture the background.
  264.             Court.Picture = Court.Image
  265.             
  266.             ' Save the screen contents where
  267.             ' the ball will go.
  268.             OffScreen.Cls
  269.             OffScreen.PaintPicture Court.Picture, _
  270.                 0, 0, D, D, oldx - r, oldy - r, D, D
  271.             OffScreen.Picture = OffScreen.Image
  272.                     
  273.         Case METHOD_SETBITS
  274.             ' Get the background image pixels.
  275.             hbm = Court.Image
  276.             status = GetObject(hbm, BITMAP_SIZE, bm)
  277.             wid = bm.bmWidthBytes
  278.             hgt = bm.bmHeight
  279.             num_bits = wid * hgt
  280.             ReDim bytes(1 To wid, 1 To hgt)
  281.             status = GetBitmapBits(hbm, num_bits, bytes(1, 1))
  282.                         
  283.     End Select
  284.     ' Start the animation.
  285.     next_time = GetTickCount()
  286.     start_time = Timer
  287.     Do While Playing
  288.         frames = frames + 1
  289.         
  290.         ' Move the ball.
  291.         newx = oldx + BallDx(moving)
  292.         If newx < r Then
  293.             newx = 2 * r - newx
  294.             BallDx(moving) = -BallDx(moving)
  295.         ElseIf newx > xmax - r Then
  296.             newx = 2 * (xmax - r) - newx
  297.             BallDx(moving) = -BallDx(moving)
  298.         End If
  299.         
  300.         newy = oldy + BallDy(moving)
  301.         If newy < r Then
  302.             newy = 2 * r - newy
  303.             BallDy(moving) = -BallDy(moving)
  304.         ElseIf newy > ymax - r Then
  305.             newy = 2 * (ymax - r) - newy
  306.             BallDy(moving) = -BallDy(moving)
  307.         End If
  308.         
  309.         ' Wait until it's time for the next frame.
  310.         next_time = next_time + mpf
  311.         WaitTill next_time
  312.         
  313.         Select Case DrawingMethod
  314.             Case METHOD_CLS
  315.                 ' Erase the screen.
  316.                 Court.Cls
  317.                 
  318.                 ' Draw the ball in its new location.
  319.                 Court.FillColor = BallClr(moving)
  320.                 Court.Circle (newx, newy), _
  321.                     r, BallClr(moving)
  322.             
  323.             Case METHOD_SAVE_AND_RESTORE
  324.                 ' Erase the area where the ball is.
  325.                 Court.PaintPicture OffScreen.Picture, _
  326.                     oldx - r, oldy - r, D, D, 0, 0, D, D
  327.                 
  328.                 ' Save the screen contents where
  329.                 ' the ball will go.
  330.                 OffScreen.PaintPicture Court.Picture, _
  331.                     0, 0, D, D, newx - r, newy - r, D, D
  332.                 OffScreen.Picture = OffScreen.Image
  333.                 
  334.                 ' Draw the ball in its new location.
  335.                 Court.FillColor = BallClr(moving)
  336.                 Court.Circle (newx, newy), _
  337.                     r, BallClr(moving)
  338.         
  339.             Case METHOD_SETBITS
  340.                 ' Erase using SetBitmapBits.
  341.                 status = SetBitmapBits(hbm, num_bits, bytes(1, 1))
  342.                 
  343.                 ' Draw the ball in its new location.
  344.                 Court.FillColor = BallClr(moving)
  345.                 Court.Circle (newx, newy), _
  346.                     r, BallClr(moving)
  347.             
  348.             Case METHOD_ERASE_AND_REDRAW
  349.                 ' Erase the moving ball.
  350.                 Court.FillColor = Court.BackColor
  351.                 Court.Circle (oldx, oldy), _
  352.                     r, Court.BackColor
  353.                 
  354.                 ' Redraw any balls that overlap
  355.                 ' the moving ball.
  356.                 For i = 2 To NumBalls
  357.                     r2 = r + BallR(i)
  358.                     If Abs(BallX(i) - oldx) <= r2 And _
  359.                        Abs(BallY(i) - oldy) <= r2 Then
  360.                         Court.FillColor = BallClr(i)
  361.                         Court.Circle _
  362.                             (BallX(i), BallY(i)), _
  363.                             BallR(i), BallClr(i)
  364.                     End If
  365.                 Next i
  366.                 
  367.                 ' Draw the ball in its new location.
  368.                 Court.FillColor = BallClr(moving)
  369.                 Court.Circle (newx, newy), _
  370.                     r, BallClr(moving)
  371.         End Select
  372.         
  373.         oldx = newx
  374.         oldy = newy
  375.     Loop
  376.     stop_time = Timer
  377.     MsgBox "Displayed" & Str$(frames) & _
  378.         " frames in " & _
  379.         Format$(stop_time - start_time, "0.00") & _
  380.         " seconds (" & _
  381.         Format$(frames / (stop_time - start_time), "0.00") & _
  382.         " FPS)."
  383.     ' Restore the old FillStyle.
  384.     Court.FillStyle = old_style
  385. End Sub
  386. ' ************************************************
  387. ' Make the ball court nice and big.
  388. ' ************************************************
  389. Private Sub Form_Resize()
  390. Const GAP = 3
  391.     FPSText.Top = ScaleHeight - GAP - FPSText.Height
  392.     Label1(0).Top = FPSText.Top
  393.     BallsText.Top = FPSText.Top - GAP - BallsText.Height
  394.     Label1(1).Top = BallsText.Top
  395.     CmdStart.Top = (BallsText.Top + FPSText.Top + FPSText.Height - CmdStart.Height) / 2
  396.     Court.Move 0, 0, ScaleWidth, BallsText.Top - GAP
  397.     xmax = Court.ScaleWidth - 1
  398.     ymax = Court.ScaleHeight - 1
  399.     InitData
  400. End Sub
  401. Private Sub Form_Unload(Cancel As Integer)
  402.     End
  403. End Sub
  404. ' ************************************************
  405. ' Save the selected drawing method.
  406. ' ************************************************
  407. Private Sub Method_Click(Index As Integer)
  408.     DrawingMethod = Index
  409. End Sub
  410. Private Sub mnuFileExit_Click()
  411.     Unload Me
  412. End Sub
  413.